package com.cloudcreativity.blyg.utils;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableField;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;

import com.cloudcreativity.blyg.R;
import com.cloudcreativity.blyg.databinding.LayoutDialogScanBinding;

public class ScanDialog extends Dialog {

    public ObservableField<String> code = new ObservableField<>();

    private OnOkListener onOkListener;
    private final ScanGunHelper helper;

    public void setOnOkListener(OnOkListener onOkListener) {
        this.onOkListener = onOkListener;
    }

    public ScanDialog(@NonNull Context context) {
        super(context, R.style.myProgressDialogStyle);
        setCanceledOnTouchOutside(true);
        LayoutDialogScanBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context),
                R.layout.layout_dialog_scan,
                null,
                false);
        assert getWindow()!=null;
        KeyBoardHelper.notAutoFocus(getWindow());
        binding.setUtils(this);
        setContentView(binding.getRoot());
        helper = new ScanGunHelper(new ScanGunHelper.OnScanValueListener() {
            @Override
            public void onScanValue(String value) {
                if(onOkListener!=null){
                    onOkListener.onOk(value);
                    KeyBoardHelper.close(getContext(),ScanDialog.this);
                    dismiss();
                }
            }
        });
    }

    @Override
    public boolean dispatchKeyEvent(@NonNull KeyEvent event) {
        helper.analysisKeyEvent(event);
        return true;
    }

    public interface OnOkListener{
        void onOk(String code);
    }
}
